package com.ecoolex.ying.space.admin.graphql;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.service.storage.*;
import com.ecoolex.framework.web.api.graphql2.annotation.*;
import com.ecoolex.ying.space.admin.core.AdminConstant;
import com.ecoolex.ying.space.entity.GoodsClassify;
import com.ecoolex.ying.space.pojo.AddGoodsClassifyDTO;
import com.ecoolex.ying.space.pojo.EditGoodsClassifyDTO;
import com.ecoolex.ying.space.pojo.QueryGoodsClassifyDTO;
import com.ecoolex.ying.space.ws.GoodsService;
import com.ecoolex.ying.space.ws.PickGoodsService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import java.util.List;

import static com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type.Admin;
import static com.ecoolex.ying.space.admin.graphql.GoodsClassifyGraphql.AUTH;

@GraphQL2Module(type = Admin, description = "商品分类管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = AUTH, name = "商品分类管理")
public class GoodsClassifyGraphql {
    static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-GOODS-CLASSIFY";
    static final String AUTH_GET = AUTH + "-GET";
    static final String AUTH_QUERY = AUTH + "-QUERY";
    static final String AUTH_ADD = AUTH + "-ADD";
    static final String AUTH_EDIT = AUTH + "-EDIT";
    static final String AUTH_DEL = AUTH + "-DEL";
    static final String AUTH_SETTING = AUTH + "-SETTING";

    static final UploadSource UPLOAD_SOURCE = new UploadSource("GoodsClassify", "Image");
    static final UploadLimit UPLOAD_LIMIT = new UploadLimit(FileSize.MB.byteLength(10), MimeType.IMAGE.commons());

    @Reference
    private GoodsService goodsService;
    @Reference
    private PickGoodsService pickGoodsService;
    @Autowired
    private StorageService storageService;

    @TypeField(type = GoodsClassify.class, field = "canEdit", description = "能否编辑")
    public boolean canEdit(@Source GoodsClassify source, AdminMemberAuthority user) {
        return user.auth(AUTH_EDIT);
    }

    @TypeField(type = GoodsClassify.class, field = "canDel", description = "能否删除")
    public boolean canDel(@Source GoodsClassify source, AdminMemberAuthority user) {
        return user.auth(AUTH_DEL);
    }

    @TypeField(type = GoodsClassify.class, field = "canSetShow", description = "能否设置展示分类")
    public boolean canSetShow(@Source GoodsClassify source) {
        return !source.getIsShow();
    }

    @TypeField(type = GoodsClassify.class, field = "canSetHide", description = "能否设置隐藏分类")
    public boolean canSetHide(@Source GoodsClassify source) {
        return source.getIsShow();
    }

    @TypeField(type = GoodsClassify.class, field = "image", description = "图片")
    public StorageInfo image(@Source GoodsClassify source) {
        return storageService.get(source.getImage());
    }

    @TypeField(type = GoodsClassify.class, field = "parent", description = "父分类")
    public GoodsClassify parent(@Source GoodsClassify source) {
        if (Check.notNullAndGtZero(source.getParentClassifyId())) {
            return goodsService.getClassifyById(source.getParentClassifyId());
        } else {
            return null;
        }
    }

    @TypeField(type = GoodsClassify.class, field = "childList", description = "子分类列表")
    public List<GoodsClassify> childList(@Source GoodsClassify source) {
        return goodsService.queryClassifyByParentId(source.getClassifyId(), null);
    }

    @TypeField(type = GoodsClassify.class, field = "usedGoodsNumber", description = "使用分类的商品数量")
    public long usedGoodsNumber(@Source GoodsClassify source) {
        return goodsService.countGoodsByBrandId(source.getClassifyId());
    }

    @TypeField(type = GoodsClassify.class, field = "usedPickGoodsNumber", description = "使用分类的优选商品数量")
    public long usedPickGoodsNumber(@Source GoodsClassify source) {
        return pickGoodsService.countPickGoodsByClassifyId(source.getClassifyId());
    }

    @QueryField(field = "goodsClassify", description = "获取商品分类详情")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "获取商品分类详情")
    public GoodsClassify get(@NonNull Integer classifyId, AdminMemberAuthority user) {
        return goodsService.getClassifyById(classifyId);
    }

    @QueryField(field = "goodsClassifyList", description = "查询商品分类列表")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "查询商品分类列表")
    public Pagination<GoodsClassify> query(@NonNull QueryGoodsClassifyDTO query, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
        user.assertAuth(AUTH_QUERY);
        return goodsService.queryClassify(query, sorts, page);
    }

    @MutationField(field = "uploadGoodsClassifyImage", description = "上传商品品牌图片")
    public StorageInfo updateImage(@NonNull Upload request, AdminMemberAuthority user) {
        if (user.auth(AUTH_ADD) || user.auth(AUTH_EDIT)) {
            return storageService.upload(request, UPLOAD_SOURCE, UPLOAD_LIMIT);
        } else {
            return user.notAuthority();
        }
    }

    @MutationField(field = "addGoodsClassify", description = "添加商品分类")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加商品分类")
    public GoodsClassify add(@NonNull AddGoodsClassifyDTO add, AdminMemberAuthority user) {
        user.assertAuth(AUTH_ADD);
        return goodsService.createClassify(add, user.userName());
    }

    @MutationField(field = "editGoodsClassify", description = "编辑商品分类")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑商品分类")
    public boolean edit(@NonNull EditGoodsClassifyDTO edit, AdminMemberAuthority user) {
        user.assertAuth(AUTH_EDIT);
        goodsService.updateClassify(edit, user.userName());
        return true;
    }

    @MutationField(field = "delGoodsClassify", description = "删除商品分类")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除商品分类")
    public boolean del(@NonNull Integer classifyId, AdminMemberAuthority user) {
        user.assertAuth(AUTH_DEL);
        GoodsClassify record = goodsService.getClassifyById(classifyId);
        Assert.isTrue(Check.notNull(record), "商品分类不存在");
        goodsService.deleteClassify(record, user.userName());
        return true;
    }

    @MutationField(field = "setShowGoodsClassify", description = "设置展示商品分类")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_SETTING, name = "设置商品分类")
    public boolean setShowGoodsClassify(@NonNull Integer classifyId, AdminMemberAuthority user) {
        user.assertAuth(AUTH_SETTING);
        GoodsClassify record = goodsService.getClassifyById(classifyId);
        Assert.isTrue(Check.notNull(record), "商品分类不存在");
        goodsService.showClassify(record, user.userName());
        return true;
    }

    @MutationField(field = "setHideGoodsClassify", description = "设置隐藏商品分类")
    public boolean setHideGoodsClassify(@NonNull Integer classifyId, AdminMemberAuthority user) {
        user.assertAuth(AUTH_SETTING);
        GoodsClassify record = goodsService.getClassifyById(classifyId);
        Assert.isTrue(Check.notNull(record), "商品分类不存在");
        goodsService.hideClassify(record, user.userName());
        return true;
    }
}
